package lablayer.model.MathComponents;

import lablayer.model.LabData;

/**
 * @author Darkness
 */

/*
 * Component #17
 *
 * Theoretical frequency of j-th interval
 */

public class TheoreticalFrequency implements MathComponent {

	private LabData result;

	private LabData data;

	private String componentName = "Теоретическая частота";

	private String componentId = "17";
	
	@Override
	public LabData getData() {
		return this.data;
	}

	@Override
	public void calculate() {
		try {
			Double[] row = this.data.getData()[0][0];
			Double[] intervalBorders;
			Double[] theoreticalFreq;
			Double intervalCount = this.data.getData()[0][1][0];
			Double mv = 0.0;
			Double sd = 0.0;
			Double il = 0.0;

			MathComponent middleValueCom = MathComponentFactory.createMiddleValue();
			middleValueCom.setData(this.data);
			middleValueCom.calculate();
			mv = middleValueCom.getResult().getData()[0][0][0];

			MathComponent standardDeviationCom = MathComponentFactory.createStandardDeviation();
			standardDeviationCom.setData(this.data);
			standardDeviationCom.calculate();
			sd = standardDeviationCom.getResult().getData()[0][0][0];

			MathComponent intLengthCom = MathComponentFactory.createIntervalLength();
			intLengthCom.setData(this.data);
			intLengthCom.calculate();
			il = intLengthCom.getResult().getData()[0][0][0];

			MathComponent intervalBordersCom = MathComponentFactory.createIntervalBorders();
			intervalBordersCom.setData(this.data);
			intervalBordersCom.calculate();
			intervalBorders = intervalBordersCom.getResult().getData()[0][0];
			theoreticalFreq = new Double[intervalBorders.length - 1];
			
			for (int i = 1; i < intervalBorders.length; i++) {
				theoreticalFreq[i - 1] = (il * (1 / (sd * Math.sqrt(2 * Math.PI)))) *
						Math.exp((Math.pow((((intervalBorders[i - 1] + intervalBorders[i]) / 2) - mv), 2)) /
						(2 * sd * sd));
			}
			this.result = new LabData(theoreticalFreq);
		} catch (Exception e) {

		}
	}

	@Override
	public void setData(LabData data) {
		try {
			assert data != null : "";
			this.data = data;
		} catch (Exception e) {}
	}

	@Override
	public String getComponentId() {
		return this.componentId;
	}

	@Override
	public String getComponentName() {
		return this.componentName;
	}

	@Override
	public LabData getResult() {
		return this.result;
	}

	@Override
	public void setParameter(String params, String values) {
		throw new UnsupportedOperationException("Not supported yet.");
	}

	@Override
	public LabData getInitData() {
		throw new UnsupportedOperationException("Not supported yet.");
	}

}
